package edu.pku.se.chocolate.UI.View;

import edu.pku.se.chocolate.ChocolateActivity;
import edu.pku.se.chocolate.R;
import edu.pku.se.chocolate.UI.Adapter.BBSlistAdapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class bbsSrcSetView extends ChocolateView{

	public static BBSlistAdapter bbsAdapter;
	public static ListView srclv;
	public static EditText srcet;
	
	
	public bbsSrcSetView(View _v, Context ct)
	{
		super(_v);
		
//		String tmpstr = new String("Joke / 笑口常开");
		
		srclv = (ListView)_v.findViewById(R.id.bbsSRClistView);		
		
		bbsAdapter = new BBSlistAdapter(mainActivity, "");
		
		EditText bbset = (EditText)_v.findViewById(R.id.bbsEditText);
		TextView topTV = (TextView)_v.findViewById(R.id.bbsTextView);
		topTV.setText("*BBS版面选择*");
		topTV.setTextAppearance(ct, android.R.style.TextAppearance_Large);
		topTV.setTextColor(Color.argb(255, 127, 255, 0));
		srclv.setAdapter(bbsAdapter);
		bbset.setHint("请输入BBS版面");	
		srcet = (EditText)self.findViewById(R.id.bbsEditText);
	}
	
	public void buildFocus()
	{
		final EditText bbset = (EditText)self.findViewById(R.id.bbsEditText);
		bbset.setFocusable(true);
		bbset.setFocusableInTouchMode(true);
	}
	
	public void moveFocus()
	{
		final EditText bbset = (EditText)self.findViewById(R.id.bbsEditText);
		bbset.setFocusable(false);
		bbset.setFocusableInTouchMode(false);
		bbset.clearFocus();
	}
	
	public void setBBSsetListener(final View _v,AddselfDefTagView _ast, final Context ct)
	{
		final AddselfDefTagView ast = _ast;
		final EditText bbset = (EditText)_v.findViewById(R.id.bbsEditText);
		
		srclv.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				String _bbs = bbsAdapter.getForumName(arg2);
//				Log.v("srclv", _bbs + "-ho");
				ast.setBBSsrc(_bbs);

//				Log.v("srclv", _bbs + "-ho");
				moveFocus();
				mainActivity.onBackPressed();
			}
			
		});
		
		srclv.setOnItemSelectedListener(new OnItemSelectedListener(){

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				String _bbs = bbsAdapter.getForumName(arg2);
//				Log.v("srclv", _bbs + "-ho");
				moveFocus();
				ast.setBBSsrc(_bbs);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		
		bbset.addTextChangedListener(new TextWatcher(){

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub
				
				String _bbs = bbset.getText().toString();
				ast.setBBSsrc(_bbs);
//				Log.v("before", "_bbs");
				
				bbsAdapter = new BBSlistAdapter(mainActivity, _bbs);
				srclv.setAdapter(bbsAdapter);
				
			}
			
		});
		
	}
	
	
	public void show()
	{
		ChocolateActivity.c_solver.setautoReflash(null,null);
		srcet.setText("");
		buildFocus();
		super.show();
	}
	
	
}
